package com.hmall.service;

import com.hdmall.business.common.common.Const;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.MallDealer;
import com.hmall.model.SysCustomer;
import com.hmall.model.SysCustomerStatement;
import com.hmall.vo.TestCustomerInformation;
import com.itextpdf.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Author  zhoutao
 */
@Service
public class TaskService {


    /**
     * 去除闭环
     */
    @Lazy
    @Autowired
    private  BackService backService;
    
    private  SysCustomerMapper sysCustomerMapper;
    
    private  SysCustomerStatementMapper sysCustomerStatementMapper;
    
    private  MallProductPlanMapper mallProductPlanMapper;
    
    private  SysDeptCustomerMapper sysDeptCustomerMapper;
    
    private  MallDealerMapper mallDealerMapper;
    
    private  SysUserMapper userMapper;
    
    private  SysUserDealerMapper sysUserDealerMapper;
    
    private  SysUserRoleMapper sysUserRoleMapper;
    
    @Autowired
    public TaskService(SysCustomerMapper sysCustomerMapper, SysCustomerStatementMapper sysCustomerStatementMapper, MallProductPlanMapper mallProductPlanMapper, SysDeptCustomerMapper sysDeptCustomerMapper, MallDealerMapper mallDealerMapper, SysUserMapper userMapper, SysUserDealerMapper sysUserDealerMapper, SysUserRoleMapper sysUserRoleMapper) {
        this.sysCustomerMapper = sysCustomerMapper;
        this.sysCustomerStatementMapper = sysCustomerStatementMapper;
        this.mallProductPlanMapper = mallProductPlanMapper;
        this.sysDeptCustomerMapper = sysDeptCustomerMapper;
        this.mallDealerMapper = mallDealerMapper;
        this.userMapper = userMapper;
        this.sysUserDealerMapper = sysUserDealerMapper;
        this.sysUserRoleMapper = sysUserRoleMapper;
    }


    /**
     * @Date 2018/11/9 10:45
     * @Param [sysCustomerList]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 对所有的客户更新上月欠款
     * 只能在每个月1号才能进行更新本月的
     * 其他时间会导致数据问题
     */
    public ServerResponse updatePaymentStatement(List<SysCustomer> sysCustomerList) throws InterruptedException {

        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;


        for(SysCustomer sysCustomer : sysCustomerList) {
            //如果这个月还没创建要进行创建
            SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectAccount(sysCustomer.getCustomerId(), year, month);

            if (null == sysCustomerStatement) {
                SysCustomerStatement customerStatement = new SysCustomerStatement();
                customerStatement.setMonth(month);
                customerStatement.setYear(year);
                customerStatement.setThisMonthReturnPayment(new BigDecimal(0));
                customerStatement.setThisMonthFufillmentPayment(new BigDecimal(0));

                //2018/9/26 从本月欠款改成了历史欠款  ini 0 -->lastPayment
                //从审核通过 改成了 发货和还款

                BigDecimal lastMonthUnpayment = new BigDecimal(0);
                BigDecimal thisMonthUnpayment = new BigDecimal(0);
                //如果上个月有欠款进行更新
                SysCustomerStatement lastCustomerStatement = sysCustomerStatementMapper.selectAccount(sysCustomer.getCustomerId(), year, month - 1);
                if (null != lastCustomerStatement) {
                    lastMonthUnpayment = lastCustomerStatement.getThisMonthUnpayment();
                    thisMonthUnpayment = lastMonthUnpayment;
                }
                customerStatement.setThisMonthUnpayment(thisMonthUnpayment);
                customerStatement.setLastMonthUnpayment(lastMonthUnpayment);
                customerStatement.setCustomerId(sysCustomer.getCustomerId());

                //有重复重复插入的问题
                sysCustomerStatementMapper.insert(customerStatement);
            }

            for(SysCustomer sysCustomer1 : sysCustomerList){
                List<SysCustomerStatement> sysCustomerStatementList = sysCustomerStatementMapper.selectList(sysCustomer1.getCustomerId(), year, month);
                if(sysCustomerStatementList.size() >= 2){
                    for(int i = 1; i < sysCustomerStatementList.size(); i++){
                        sysCustomerStatementMapper.delete(sysCustomerStatementList.get(i));
                    }
                }
            }


        }
        return ServerResponse.createBySuccess();
    }

    /**
     * @Date 2018/11/9 10:45
     * @Param [sysCustomerList]
     * @return com.hmall.common.ServerResponse
     * @Description // 原则上一个月一次
     */
    public ServerResponse createImg(List<SysCustomer> sysCustomerList) throws FileNotFoundException, DocumentException {

        //每个月月初生成上个月的发货表单
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;
        calendar.set(year, month, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Integer date = calendar.get(Calendar.DATE);


        for(int i = 1; i <= 12; i++){
            //加入当前是九月 不能生成九月的 2018-9 --- 2018-10
            if(i >= month) {
                continue;
            }
            updateStatementImg(sysCustomerList, year, i, date);
        }
        return ServerResponse.createBySuccess();
        }

    public ServerResponse changePlanStatus(Long planNo, Integer status) {

        mallProductPlanMapper.updatePlan(mallProductPlanMapper.selectByPlanNo(planNo).getPlanId(), status);
        return ServerResponse.createBySuccess();

    }

    /**
     * @Date 2018/11/9 10:45
     * @Param [sysCustomerList]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 当前月份的图片
     *  20min 一次
     */
    public ServerResponse createImgNow(List<SysCustomer> sysCustomerList) throws FileNotFoundException, DocumentException {

        //每个月月初生成上个月的发货表单
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;
        calendar.set(year, month, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Integer date = calendar.get(Calendar.DATE);


        for(int i = 1; i <= 12; i++){
            //加入当前是九月 不能生成九月的 2018-9 --- 2018-10
            if(i != month) {
                continue;
            }
            //更新对账单图片
            updateStatementImg(sysCustomerList, year, i, date);

        }
        return ServerResponse.createBySuccess();
    }

    /**
     * @Date 2018/11/9 10:48
     * @Param [sysCustomerList, year, month, date]
     * @return void
     * @Description // 更新对账单图片
     */
    private void updateStatementImg(List<SysCustomer> sysCustomerList, Integer year, Integer month, Integer date) throws FileNotFoundException, DocumentException {
        for(SysCustomer sysCustomer : sysCustomerList){
            SysCustomerStatement sysCustomerStatement = sysCustomerStatementMapper.selectAccount(sysCustomer.getCustomerId(), year, month);
            if(null == sysCustomerStatement) {
                continue;
            }
            String start = year + "-" + month + "-" + 1;
            String end = year + "-" + (month) + "-" + date;
            String img = (String) backService.createOrderFufillmentPDF(null, sysCustomer.getCustomerId(), start, end).getData();
            sysCustomerStatementMapper.updateImg(sysCustomer.getCustomerId(), year, month, img);
        }
    }

    public ServerResponse<TestCustomerInformation> testGetMoreInformationAboutCustomer(String customerName) {

        TestCustomerInformation testCustomerInformation = new TestCustomerInformation();
        SysCustomer customer = sysCustomerMapper.selectByLikeCustomerName(customerName);
        MallDealer mallDealer = mallDealerMapper.selectByPrimaryKey(customer.getDealerId());
        testCustomerInformation.setSysCustomer(customer);
        testCustomerInformation.setDealerId(customer.getDealerId());
        testCustomerInformation.setDealerName(mallDealer.getDealerName());
        Integer deptId = sysDeptCustomerMapper.selectByCustomerId(customer.getCustomerId());
        testCustomerInformation.setDeptId(deptId);
        testCustomerInformation.setSalesmanUsername(userMapper.selectByPrimaryKey(customer.getSalesmanId()).getUsername());

        //sys_user_dealer下所有都是跟单员
        List<Integer> merchUserIdList = sysUserDealerMapper.selectByDealerId(mallDealer.getDealerId());
        List<String> merchUserNameList = new ArrayList<>();
        for(Integer merchUserId : merchUserIdList){
            merchUserNameList.add(userMapper.selectByPrimaryKey(merchUserId).getUsername());
        }
        testCustomerInformation.setMerchUsernameList(merchUserNameList);

        //找到所有的资金
        List<Integer> financeUserIdList = sysUserRoleMapper.selectUserIdByRole(Const.FINANCE_ROLE_ID);
        List<String> financeUserNameList = new ArrayList<>();
        for(Integer financeUserId : financeUserIdList){
            financeUserNameList.add(userMapper.selectByPrimaryKey(financeUserId).getUsername());
        }

        testCustomerInformation.setFinanceUsernameList(financeUserNameList);

        return ServerResponse.createBySuccess(testCustomerInformation);
    }
}
